package com.example.demo.controller;

import com.example.demo.test.User;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("return")
public class ReturnController {
//    @RequestMapping("/t1")
//    public String test1() {
//        return "/index.html";
//    }

    @ResponseBody
    @RequestMapping("/t2")
    public String t2() {
        return "Hello World";
    }
//    返回html片段
    @ResponseBody
    @RequestMapping("/t3")
    public String t3() {
        return "<h3>我是返回的片段<h3>";
    }

    @ResponseBody
    @RequestMapping("/t4")
//    自动返回成json格式
    public User t4() {
        User user = new User();
        user.setAge(18);
        user.setId(1);
        user.setName("haha");
        return user;
    }


    @ResponseBody
    @RequestMapping("/t5")
//    设置状态码
    public String t5(HttpServletResponse response) {
       response.setStatus(402);
       return "设置状态码成功！";
    }
//   设置返回内容的指定格式,也可以同步设置编码
    @ResponseBody
    @RequestMapping(value = "/t6",produces = "application/json")
        public String t6(HttpServletResponse response) {
        return "{\"OK\":1}";
    }

//    设置Header
    @ResponseBody
    @RequestMapping("/t7")
    public String t7(HttpServletResponse response) {
        response.setHeader("myHeader","value");
        return "设置Header成功";
    }

}
